home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
379_01
/
zoo210s.zoo
/
portable.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-12
|
22KB
|
723 lines
#ifndef LINT
/* @(#) portable.c 2.24 88/08/24 01:22:06 */
static char sccsid[]="@(#) portable.c 2.24 88/08/24 01:22:06";
#endif /* LINT */
#include "options.h"
/*
Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
(C) Copyright 1988 Rahul Dhesi -- All rights reserved
*/
/**********************
portable.c contains functions needed to make Zoo portable to various
implementations of C.
Note: Provided a 2's complement machine is used, all functions in
this file are themselves machine-independent and need not be changed
when implementing Zoo on a different machine. Some code will choke
on 1's complement machines--I think.
For machine-dependent declarations see files "machine.h" and "options.h".
For machine-dependent functions see file "machine.c"
*/
#include "zoo.h"
#include "zooio.h"
#include "various.h"
#include "zoofns.h"
#include "machine.h"
#include "debug.h"
#include "assert.h"
#ifdef NEEDCTYP
#include <ctype.h> /* for tolower() */
#endif
#include "portable.h"
#ifdef TRACE_IO
extern int verbose;
#endif
/* Functions defined for use within this file only. */
long to_long PARMS((BYTE[]));
int to_int PARMS((BYTE[]));
void b_to_zooh PARMS((struct zoo_header *, BYTE[]));
void b_to_dir PARMS((struct direntry *, BYTE[]));
int dir_to_b PARMS((BYTE[], struct direntry *));
void zooh_to_b PARMS((BYTE[], struct zoo_header *));
void splitlong PARMS((BYTE[], long));
void splitint PARMS((BYTE[], int));
#ifdef TRACE_IO
void show_h PARMS ((struct zoo_header *));
void show_dir PARMS ((struct direntry *));
#endif /* TRACE_IO */
extern unsigned int crccode;
/************************************************************************/
/* I/O functions */
/************************************************************************/
/* some functions get defined only if they aren't already macros */
#ifndef zooread
int zooread (file, buffer, count)
ZOOFILE file; char *buffer; int count;
{ return (fread (buffer, 1, count, file)); }
#endif /* zooread */
#ifndef FIZ
#ifndef zoowrite
int zoowrite (file, buffer, count)
ZOOFILE file; char *buffer; int count;
{
if (file == NULLFILE)
return (count);
else
return (fwrite (buffer, 1, count, file));
}
#endif /* zoowrite */
ZOOFILE zoocreate (fname)
char *fname;
{ return ((ZOOFILE) fopen (fname, Z_NEW)); }
#endif /* FIZ */
#ifndef zooseek
long zooseek (file, offset, whence)
ZOOFILE file; long offset; int whence;
{ return (fseek (file, offset, whence)); }
#endif /* zooseek */
ZOOFILE zooopen (fname, option)
char *fname; char *option;
{ return ((ZOOFILE) fopen (fname, option)); }
#ifndef zootell
long zootell (file)
ZOOFILE file;
{ return ftell (file); }
#endif /* zootell */
int zooclose (file)
ZOOFILE file;
{ return fclose (file); }
/**********************
low_ch() is a macro that returns a lowercased char; it may be
used with any char, whether or not it is uppercase. It will
be used below by one or two functions.
*/
#define low_ch(c) (isupper(c) ? tolower(c) : c)
/************************************************************************/
/*** Following are functions that make up for various implementations ***/
/*** of C not having certain library routines. ***/
/************************************************************************/
#ifndef FIZ
/**********************
str_lwr() converts a string to lowercase and returns a pointer to the string
*/
char *str_lwr (str)
char *str;
{
register char *s;
s = str;
while (*s != '\0') {
*s = toascii(*s);
*s = low_ch(*s);
s++;
}
return (str);
}
/**********************
str_icmp() compares strings just like strcmp() but it does it without regard to
case.
*/
int str_icmp (s1, s2)
register char *s1, *s2;
{
for ( ; low_ch(*s1) == low_ch(*s2); s1++, s2++)
if (*s1 == '\0')
return(0);
return(low_ch(*s1) - low_ch(*s2));
}
#ifdef NEED_MEMSET
/**********************
memset() it sets the first "count" bytes of "dest" to the character
"c" and returns a pointer to "dest".
*/
VOIDPTR memset (dest, c, count)
register VOIDPTR dest;
int c;
unsigned count;
{
register unsigned i;
for (i = 0; i < count; i++) {
*((char *) (dest + i)) = c;
}
return dest;
}
#endif /* NEED_MEMSET */
#ifdef NEED_MEMCPY
/**********************
memcpy() copies "count" bytes from "src" to "dest" and returns
a pointer to "dest". Not necessarily safe for overlapping moves. */
VOIDPTR memcpy(dest, src, count)
register VOIDPTR dest;
register VOIDPTR src;
unsigned count;
{
VOIDPTR savedest = dest;
while (count > 0) {
*((char *) dest++) = *((char *) src++);
count--;
}
}
#endif /* NEED_MEMCPY */
#ifndef FPUTCHAR
/**********************
fputchar() writes a character to stdout. It is identical to putchar
but is a function, not a macro.
*/
int fputchar (c)
int c;
{
return (fputc(c, stdout));
}
#endif /* FPUTCHAR */
#endif /* FIZ */
/***********************************************************************/
/*** Following are declarations and functions that are written in a ***/
/*** machine-independent way but they implement machine-dependent ***/
/*** activities ***/
/***********************************************************************/
#ifndef DIRECT_CONVERT
/**********************
to_long() converts four consecutive bytes, in order of increasing
significance, to a long integer. It is used to make Zoo independent of the
byte order of the system.
*/
long to_long(data)
BYTE data[];
{
return (long) ((unsigned long) data[0] | ((unsigned long) data[1] << 8) |
((unsigned long) data[2] << 16) | ((unsigned long) data[3] << 24));
}
#ifndef FIZ
/********************
splitlong() converts a long integer to four consecutive BYTEs in order
of increasing significance.
*/
void splitlong(bytes, bigword)
BYTE bytes[];
long bigword;
{
int i;
for (i = 0; i < 4; i++) {
bytes[i] = bigword & 0xff;
bigword = (unsigned long) bigword >> 8;
}
}
#endif /* FIZ */
/*******************
splitint() converts an integer to two consecutive BYTEs in order
of increasing significance.
*/
void splitint(bytes, word)
BYTE bytes[];
int word;
{
bytes[0] = word & 0xff;
word = (unsigned int) word >> 8;
bytes[1] = word & 0xff;
}
/**********************
to_int() converts two consecutive bytes, in order of increasing
significance, to an integer, in a machine-independent manner
*/
int to_int(data)
BYTE data[];
{
return (int) ((unsigned int) data[0] | ((unsigned int) data[1] << 8));
}
#else /* else of ifndef DIRECT_CONVERT */
long to_long(data)
BYTE data[];
{
return ( * (long *) data );
}
#ifndef FIZ
/********************
splitlong() converts a long integer to four consecutive BYTEs in order
of increasing significance.
*/
void splitlong(bytes, bigword)
BYTE bytes[];
long bigword;
{
* (long *) bytes = bigword;
}
#endif /* FIZ */
/*******************
splitint() converts an integer to two consecutive BYTEs in order
of increasing significance.
*/
void splitint(bytes, word)
BYTE bytes[];
int word;
{
* (int *) bytes = word;
}
/**********************
to_int() converts two consecutive bytes, in order of increasing
significance, to an integer.
*/
int to_int(data)
BYTE data[];
{
return (*(int *) data);
}
#endif /* ifndef DIRECT_CONVERT .. else ... */
#ifndef FIZ
/**********************
Function frd_zooh() reads the header of a Zoo archive in a machine-
independent manner, from a ZOOFILE.
*/
int frd_zooh(zoo_header, zoo_file)
struct zoo_header *zoo_header;
ZOOFILE zoo_file;
{
int status;
BYTE bytes[SIZ_ZOOH]; /* canonical header representation */
#ifdef TRACE_IO
if (verbose) {
printf("At file position [%8lx] ", ftell(zoo_file));
}
#endif
status = zooread (zoo_file, (char *) bytes, SIZ_ZOOH);
b_to_zooh (zoo_header, bytes); /* convert array to str